package com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord;

import cn.hutool.core.collection.CollectionUtil;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.api.room.RoomApi;
import com.nanhai.buddhist.college.module.collegebase.api.room.dto.RoomRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.study.controller.admin.classclockrecord.vo.*;
import com.nanhai.buddhist.college.module.study.dal.dataobject.classclockrecord.ClockRecordDO;
import com.nanhai.buddhist.college.module.study.convert.classclockrecord.ClockRecordConvert;
import com.nanhai.buddhist.college.module.study.service.classclockrecord.ClockRecordService;

@Api(tags = "管理后台 - 打卡记录")
@RestController
@RequestMapping("/study/clock-record")
@Validated
public class ClockRecordController {

    @Resource
    private ClockRecordService clockRecordService;

    @Resource
    private RoomApi roomApi;

    @PostMapping("/create")
    @ApiOperation("创建打卡记录")
    @PreAuthorize("@ss.hasPermission('study:clock-record:create')")
    public CommonResult<String> createClockRecord(@Valid @RequestBody ClockRecordCreateReqVO createReqVO) {
        return success(clockRecordService.createClockRecord(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新打卡记录")
    @PreAuthorize("@ss.hasPermission('study:clock-record:update')")
    public CommonResult<Boolean> updateClockRecord(@Valid @RequestBody ClockRecordUpdateReqVO updateReqVO) {
        clockRecordService.updateClockRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除打卡记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('study:clock-record:delete')")
    public CommonResult<Boolean> deleteClockRecord(@RequestParam("id") String id) {
        clockRecordService.deleteClockRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得打卡记录")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('study:clock-record:query')")
    public CommonResult<ClockRecordRespVO> getClockRecord(@RequestParam("id") String id) {
        ClockRecordDO clockRecord = clockRecordService.getClockRecord(id);
        return success(ClockRecordConvert.INSTANCE.convert(clockRecord));
    }

    @GetMapping("/list")
    @ApiOperation("获得打卡记录列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    @PreAuthorize("@ss.hasPermission('study:clock-record:query')")
    public CommonResult<List<ClockRecordRespVO>> getClockRecordList(@RequestParam("ids") Collection<String> ids) {
        List<ClockRecordDO> list = clockRecordService.getClockRecordList(ids);

        List<ClockRecordRespVO> clockRecordRespVOS = ClockRecordConvert.INSTANCE.convertList(list);

        return success(clockRecordRespVOS);
    }

    @GetMapping("/page")
    @ApiOperation("获得打卡记录分页")
    @PreAuthorize("@ss.hasPermission('study:clock-record:query')")
    public CommonResult<PageResult<ClockRecordRespVO>> getClockRecordPage(@Valid ClockRecordPageReqVO pageVO) {
        PageResult<ClockRecordDO> pageResult = clockRecordService.getClockRecordPage(pageVO);
        PageResult<ClockRecordRespVO> clockRecordRespVOPageResult = ClockRecordConvert.INSTANCE.convertPage(pageResult);

        // 回显 教室名称
        Set<String> roomIds = CollectionUtils.convertSet(clockRecordRespVOPageResult.getList(), ClockRecordRespVO::getRoomId);
        if(CollectionUtil.isNotEmpty(roomIds)){
            CommonResult<List<RoomRespDTO>> classroomInfoList = roomApi.getClassroomInfoList(roomIds);
            Map<String, RoomRespDTO> stringRoomRespDTOMap = CollectionUtils.convertMap(classroomInfoList.getData(), RoomRespDTO::getId);
            clockRecordRespVOPageResult.getList().stream().forEach(item->{
                item.setRoomName(Optional.ofNullable(stringRoomRespDTOMap).map(s->s.get(item.getRoomId())).map(RoomRespDTO::getClassroomName).orElse(""));
            });

        }
        return success(clockRecordRespVOPageResult);
    }



}
